<%
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
%>

<% css_bundle :submission %>

<% user_name = params[:hide_student_name] ? t(:anonymous_username, "This Student") : submission_author_name_for(@assessment_request) %>
<% content_for :page_title do %><%= join_title @assignment.title, submission_author_name_for(@assessment_request) %><% end %>

<% if @assignment.submission_types == 'discussion_topic' && @assignment.discussion_topic %>
  <div style="width: 600px; margin: 10px auto;">
    <p>
      <% if assignment_visible_to_user = @assignment.visible_to_user?(@current_user)%>
        <% if @assignment.discussion_topic.for_group_discussion? && (group = @assignment.discussion_topic.group_category.group_for(@user)) %>
          <%= t('group_discussion_submission_description',
                  "The submissions for this assignment are posts in the assignment's discussion for this group. Below are the discussion posts for %{user}, or you can *view the full group discussion*.",
                  :user => user_name,
                  :wrapper => "<a id='discussion_view_link' href=\"#{context_url(group, :context_discussion_topics_url, :root_discussion_topic_id => @assignment.discussion_topic.id, :hide_student_names => params[:hide_student_name], :student_id => @submission.user.id, :headless => 1)}\"><b>\\1</b></a>") %>
        <% else %>
          <%= t('discussion_submission_description',
                  "The submissions for this assignment are posts in the assignment's discussion. Below are the discussion posts for %{user}, or you can *view the full discussion*.",
                  :user => user_name,
                  :wrapper => "<a id='discussion_view_link' href=\"#{context_url(@assignment.context, :context_discussion_topic_url, @assignment.discussion_topic.id, :hide_student_names => params[:hide_student_name], :student_id => @submission.user.id, :headless => 1)}\"><b>\\1</b></a>") %>
        <% end %>
      <% else %>
        <% if @assignment.discussion_topic.for_group_discussion? && (group = @assignment.discussion_topic.group_category.group_for(@user)) %>
          <%= t('group_discussion_submission_description_without_link',
                  "The submissions for this assignment are posts in the assignment's discussion for this group. Below are the discussion posts for %{user}.",
                  :user => user_name) %>
        <% else %>
          <%= t('discussion_submission_description_without_link',
                  "The submissions for this assignment are posts in the assignment's discussion. Below are the discussion posts for %{user}.",
                  :user => user_name) %>
        <% end %>
      <% end %>
    </p>
    <% @entries = @assignment.discussion_topic.discussion_entries.active.for_user(@user) %>
    <% if @assignment.discussion_topic.for_group_discussion? %>
      <% @entries = @assignment.discussion_topic.child_topics.map{|t| t.discussion_entries.active.for_user(@user) }.flatten.sort_by{|e| e.created_at} %>
    <% end %>
    <% @entries.each do |entry| %>
      {{> views/discussion_topics/entry}}
    <% end %>
    <div style="text-align: center; font-size: 1.2em; margin-top: 10px; display: none;">
      <a href="<%= context_url(@assignment.context, :context_discussion_topic_url, @assignment.discussion_topic.id, :headless => 1, :combined => 1) %>" class="forward">{{#t}}Show the Entire Discussion{{/t}}</a>
    </div>
  </div>
<% elsif @submission.submission_type == "online_url" %>
  <div style="text-align: center; width: 500px; margin: 10px auto; font-size: 0.8em;">
    <% image = @submission.attachment if @submission.processed? %>
    {{#t}}This submission was a URL to an external page.{{/t}}
    <% if image %>
      {{#t}}We've included a snapshot of what the page looked like when it was submitted.{{/t}}
    <% else %>
      {{#t}}Keep in mind that this page may have changed since the submission originally occurred.{{/t}}
    <% end %>
    <div style="font-size: 1.4em; margin: 10px;">
      <%= link_to @submission.url,
                  @submission.url,
                  class: "not_external" %>
    </div>
    <div style="font-size: 1.4em; margin: 10px;">
      <%= link_to t('view_in_a_new_tab', '(View in a new tab)'),
                  @submission.url,
                  class: "open_in_a_new_tab",
                  target: "_blank" %>

      <span style="display: inline-block;" class="ui-icon ui-icon-extlink ui-icon-inline"
        title="{{#t}}Links to an external site.{{/t}}"/>
    </div>
  </div>
  <div style="text-align: center;">
  <% if @submission.processed %>
    <img src="<%= context_url(@context, :context_assignment_submission_url, @assignment.id, @submission.user_id, :download => image.id) %>" alt="{{#t}}URL Preview{{/t}}"/>
  <% end %>
  </div>
<% elsif @submission.submission_type == "online_text_entry" %>
  <% js_bundle 'legacy/submissions_show_preview_text' %>
  <div style="text-align: right; margin-top: 15px;">
    <select class="data_view">
      <option aria-label="<%= t("Assignment %{assignment} submission Paper View", :assignment=> @assignment.title) %>" value="paper">{{#t}}Paper View{{/t}}</option>
      <option aria-label="<%= t("Assignment %{assignment} submission Plain Text View", :assignment=> @assignment.title) %>" value="plain_text">{{#t}}Plain Text View{{/t}}</option>
    </select>
  </div>
  <div id="submission_preview" class="paper user_content">
    <%= user_content(@submission.body) %>
  </div>
<% elsif @submission.submission_type == "online_upload" %>

  <% if @submission.versioned_attachments.empty? %>
    <div class="file-upload-submission">
      <span>
        {{#t}}No files were uploaded for this submission{{/t}}
      </span>
    </div>
  <% else %>

    <% @submission.versioned_attachments.each do |attachment| %>
      <div class="file-upload-submission">

        <div class="file-upload-submission-info">
          <% preview_url = context_url(@context, :context_assignment_submission_url, @assignment.id, @submission.user_id, :download => attachment.id) %>
          <a href="<%= preview_url %>" class="<%= attachment.mime_class %> file-upload-submission-info__link">
            <%= attachment.display_name %>
          </a>
          <span class="file-upload-submission-info__filesize"><%= number_to_human_size(attachment.size) %></span>
        </div>

        <div class="file-upload-submission-turnitin">
          <% if @submission.can_view_plagiarism_report('vericite', @current_user, session) && @submission.turnitin_data[:provider] == :vericite && (vericite_score = @submission.vericite_data(true)[attachment.asset_string]) && vericite_score[:similarity_score] %>

            <span class="turnitin_score_container">
              <span class="vericite_score_container_caret <%= vericite_score[:state] %>_score"></span>
              <a href="<%= context_url(@context, :context_assignment_submission_vericite_report_url, @submission.assignment_id, @submission.user_id, attachment.asset_string) %>" target="_blank" title="Similarity score -- more information" class="tooltip not_external turnitin_similarity_score <%= vericite_score[:state] %>_score">
                <%= vericite_score[:similarity_score] %>%
                <span class="tooltip_wrap right">
                  <span class="tooltip_text">{{#t}}See VeriCite results{{/t}}</span>
                </span>
              </a>
            </span>

          <% elsif can_do(@submission, @current_user, :view_turnitin_report) &&
            (turnitin_score = @submission.originality_data[attachment.asset_string]) &&
            (@submission.originality_data[:provider] == nil || @submission.originality_reports.present?) &&
            turnitin_score[:similarity_score] %>
            <span class="turnitin_score_container">
              <span class="turnitin_score_container_caret <%= turnitin_score[:state] %>_score"></span>
              <% if @submission.originality_reports.blank? %>
                <a href="<%= context_url(@context, :context_assignment_submission_turnitin_report_url, @submission.assignment_id, @submission.user_id, attachment.asset_string) %>" title="Similarity score -- more information" target="_blank" class="not_external turnitin_similarity_score <%= turnitin_score[:state] %>_score"><%= turnitin_score[:similarity_score] %>%</a>
              <% else %>
                <a href="<%= polymorphic_url([@context, @assignment, @submission, :originality_report], asset_string: attachment.asset_string) %>" target="_blank" title="Similarity score -- more information" class="not_external turnitin_similarity_score <%= turnitin_score[:state] %>_score"><%= turnitin_score[:similarity_score] %>%</a>
              <% end %>
            </span>
          <% end %>
        </div>

        <div class="file-upload-submission-attachment">
          <% if attachment.crocodoc_available? || attachment.canvadoc.try(:has_annotations?) %>
              <% preview_document = t('preview_crocodoc_document', 'Preview your submission and view teacher feedback, if available')
                 button_text = t('view_feedback_button', 'View Feedback')
              %>
          <% elsif attachment.canvadocable? %>
            <% preview_document = t('preview_submission', 'Preview your submission')
               button_text = t('view_preview_button', 'Preview')
            %>
          <% end %>
          <% if attachment.crocodoc_available? || attachment.canvadocable? %>
            <a href=<%= preview_url %> data-tooltip title="<%= preview_document %>" class="modal_preview_link Button--link" role="button" data-attachment_id="<%= attachment.id %>" data-submission_id="<%= @submission.id %>" data-dialog-title="<%= attachment.display_name %>" <%= doc_preview_attributes(attachment, enable_annotations: true, moderated_grading_whitelist: @moderated_grading_whitelist) %>><%= button_text %></a>
          <% end %>
        </div>

      </div>
    <% end %>

  <% end %>

  <% js_bundle 'legacy/submissions_show_preview_upload' %>
<% elsif @submission.submission_type == "online_quiz" %>
  <% content_for :stylesheets do %>
    <meta HTTP-EQUIV="REFRESH" content="0; url=<%= context_url(@context, :context_quiz_history_url, @assignment.quiz.id, :user_id => @submission.user_id, :hide_student_name => params[:hide_student_name], :headless => 1, :version => @submission.quiz_submission_version) %>"/>
  <% end %>
  <div style="text-align: center;">
    {{#t}}This submission was a quiz submission.  Redirecting to quiz page.{{/t}}
  </div>
<% elsif @submission.submission_type == 'media_recording' && @submission.media_comment_id %>
  {{#t}}media_comment_link', "This submission is a media recording. *click here to view.*", :wrapper => '<div><a href="#" class="play_media_recording_link" '+ dataify(@submission, :media_comment_id, :media_comment_type) + '>\1</a></div>{{/t}}
  <div style="width: 400px; margin: 10px auto;" id="media_recording_box">
    <div class="box_content">
    </div>
  </div>
  <div class="media_comment_id" style="display: none;"><%= @submission.media_comment_id %></div>

  <% js_bundle 'legacy/submissions_show_preview_media' %>
<% elsif @submission.submission_type == 'basic_lti_launch' %>
  <meta HTTP-EQUIV="REFRESH" content="0; url=<%= retrieve_course_external_tools_url(@context.id, assignment_id: @assignment.id, display: 'borderless', url: @submission.external_tool_url) %>"/>
<% elsif @submission.has_submission? || @assignment.expects_external_submission? %>
  <div style="margin: 20px auto; width: 400px; height: 50px; font-size: 1.5em;  font-weight: bold;">
    {{#t}}No Preview Available{{/t}}
  </div>
<% else %>
  <div style="margin: 20px auto; width: 200px; height: 50px; font-size: 1.5em;  font-weight: bold;">
    {{#t}}No Submission{{/t}}
  </div>
<% end %>
